<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item prop="avatar" label-width="0px">
            <image-upload v-model="form.avatar" @input="handleImageInput($event, 'avatar')" :isShowTip="false"
              :limit="1" />
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-row>
            <el-col :span="8">
              <el-form-item label="姓名" prop="staffName">
                <el-input v-model="form.staffName" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="性别" prop="staffSex">
                <el-radio-group v-model="form.staffSex">
                  <el-radio v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                    :value="dict.value"></el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工服尺码" prop="clothesSize">
                <el-input v-model="form.clothesSize" placeholder="请输入">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="手机号" prop="staffPhone">
                <el-input v-model="form.staffPhone" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="职别" >
                <el-select v-model="form.staffLevels" placeholder="请选择">
                  <el-option label="无" value="0"></el-option>
                  <el-option :key="item.jobCategoryId" v-for="item in categoryOptions" :label="item.jobName"
                    :value="item.jobCategoryId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="入职时间" prop="employmentTime">
                <el-date-picker type="date"  value-format="yyyy-MM-dd" placeholder="选择日期" v-model="form.employmentTime"
                  style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="银行卡号" prop="bankCard">
                <el-input v-model="form.bankCard" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="身份证号" prop="staffIdcard">
                <el-input v-model="form.staffIdcard" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否挂岗" prop="isPrepared">
                <el-radio-group v-model="form.isPrepared">
                  <el-radio :label="1">是</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="紧急联系人" prop="urgentLinkman">
                    <el-input v-model="form.urgentLinkman" placeholder="请输入"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label-width="0px" prop="urgentLinkmanPhone">
                    <el-input v-model="form.urgentLinkmanPhone" placeholder="请输入紧急联系人手机号码"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
            <el-col :span="12">
              <el-form-item label="家庭住址" prop="staffAddress">
                <el-input v-model="form.staffAddress" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <el-row :gutter="10">
        <el-col :span="6">
          <div class="image_title">身份证正面</div>
          <el-form-item prop="imgIdcardPositive" label-width="0px">
            <image-upload v-model="form.imgIdcardPositive" @input="handleImageInput($event, 'imgIdcardPositive')"
              :isShowTip="false" :limit="1" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <div class="image_title">身份证反面</div>
          <el-form-item prop="imgIdcardOppsite" label-width="0px">
            <image-upload v-model="form.imgIdcardOppsite" @input="handleImageInput($event, 'imgIdcardOppsite')"
              :isShowTip="false" :limit="1" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <div class="image_title">银行卡正面</div>
          <el-form-item prop="imgBankPositive" label-width="0px">
            <image-upload v-model="form.imgBankPositive" @input="handleImageInput($event, 'imgBankPositive')"
              :isShowTip="false" :limit="1" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <div class="image_title">银行卡反面</div>
          <el-form-item prop="imgBankOppsite" label-width="0px">
            <image-upload v-model="form.imgBankOppsite" @input="handleImageInput($event, 'imgBankOppsite')"
              :isShowTip="false" :limit="1" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <el-row :gutter="10">
        <el-col :span="12">
          <div class="image_title">合同照片</div>
          <el-form-item prop="contractImg" label-width="0px">
            <image-upload v-model="form.contractImg" @input="handleImageInput($event, 'contractImg')" :isShowTip="false"
              :limit="4" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <div class="image_title">管理岗协议</div>
          <el-form-item prop="agreementImg" label-width="0px">
            <image-upload v-model="form.agreementImg" @input="handleImageInput($event, 'agreementImg')"
              :isShowTip="false" :limit="2" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <el-row :gutter="10">
        <el-col :span="12">
          <div class="image_title">履历表</div>
          <el-form-item prop="resumeImg" label-width="0px">
            <image-upload v-model="form.resumeImg" @input="handleImageInput($event, 'resumeImg')" :isShowTip="false"
              :limit="1" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>

import { getListCategory } from '@/api/cld/category'
import { getStaff, addStaff, updateStaff } from '@/api/cld/staff'
import { idCardValid, validatePhone, bankAccountValid } from '@/utils/validataForm'
export default {
  components: {
  },
  props: {
    staffId: {
      type: Number
    }
  },
  dicts: ['sys_user_sex'],
  data() {
    return {
      // 职别列表
      categoryOptions: [],
      form: {},
      rules: {
        // avatar: [
        //   { required: true, message: "请上传头像", trigger: "blur" },
        // ],
        staffName: [
          { required: true, message: "请输入员工姓名", trigger: "blur" },
        ],
        staffSex: [
          { required: true, message: "请选择员工性别", trigger: "blur" },
        ],
        // clothesSize: [
        //   { required: true, message: "请选择工服尺码", trigger: "blur" },
        // ],
        staffPhone: [
          { required: true, validator: validatePhone, trigger: "blur" },
        ],
        // employmentTime: [
        //   { required: true, message: "请选择入职时间", trigger: "blur" },
        // ],
        // bankCard: [
        //   { required: true, validator: bankAccountValid, trigger: "blur" },
        // ],
        staffIdcard: [
          { required: true, validator: idCardValid, trigger: "blur" },
        ],
        // urgentLinkman: [
        //   { required: true, message: "请输入紧急联系人", trigger: "blur" },
        // ],
        // urgentLinkmanPhone: [
        //   { required: true, validator: validatePhone, trigger: "blur" },
        // ],
        // staffAddress: [
        //   { required: true, message: "请输入家庭住址", trigger: "blur" },
        // ],
        // imgBankPositive: [
        //   { required: true, message: "请上传银行卡正面", trigger: "blur" },
        // ],
        // imgBankOppsite: [
        //   { required: true, message: "请上传银行卡反面", trigger: "blur" },
        // ],
        // imgIdcardPositive: [
        //   { required: true, message: "请上传身份证正面", trigger: "blur" },
        // ],
        // imgIdcardOppsite: [
        //   { required: true, message: "请上传身份证反面", trigger: "blur" },
        // ],
        // contractImg: [
        //   { required: true, message: "请上传合同照片", trigger: "blur" },
        // ],
        // agreementImg: [
        //   { required: true, message: "请上传协议照片", trigger: "blur" },
        // ],
      },
    };
  },
  computed: {
  },
  watch: {
  },
  created() {
    this.reset();
    if (this.staffId) {
      this.getStaffInfo();
    }else{
      this.getConfigKey("sys.user.initPassword").then(response => {
       this.form.staffPassword = response.msg;
      });
    }
    // 获取职别列表
    this.handleGetListCategory();
  },
  mounted() { },
  methods: {
    // 获取员工详情
    getStaffInfo() {
      getStaff(this.staffId).then(res => {
        this.form = res.data;
      })
    },
    // 职别列表
    handleGetListCategory() {
      getListCategory().then(res => {
        this.categoryOptions = res.data;
      })
    },
    // 图片上传回调
    handleImageInput(e, field) {
      this.form[field] = e;
    },
    // 表单重置
    reset() {
      this.form = {
        staffId: null,
        staffNo: null,
        staffName: null,
        staffSex: null,
        bankCard: null,
        staffPhone: null,
        staffIdcard: null,
        urgentLinkman: null,
        urgentLinkmanPhone: null,
        staffAddress: null,
        recommenStaffId: null,
        registerTime: null,
        employmentTime: null,
        postTime: null,
        staffStatus: null,
        imgBankPositive: null,
        imgBankOppsite: null,
        imgIdcardPositive: null,
        imgIdcardOppsite: null,
        resignTime: null,
        resignReason: null,
        resignTableImg: null,
        approvalId: null,
        approvalTime: null,
        createId: null,
        createName: null,
        createTime: null,
        flag: null,
        staffContractId: null,
        contractImg: null,
        agreementImg: null,
        resumeImg: null,
        resumePrintStatus: null,
        blackReason: null,
        clothesSize: null,
        shoesSize: null,
        insuranceId: null,
        staffLevels: null,
        isPrepared: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.staffId != null) {
            updateStaff(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.$emit('success');
            });
          } else {
            addStaff(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.$emit('success');
            });
          }
        }
      });
    },
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}

.el-divider--horizontal {
  margin: 5px 0;
}

.image_title {
  margin-left: 35px;
  margin-bottom: 5px;
  font-weight: 600;
}

.app-container {
  padding: 0px;
}

.el-form-item--mini.el-form-item {
  margin-bottom: 10px;
}
</style>
